/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.suli.roll.rest;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import me.zhengjie.annotation.Log;
import me.zhengjie.annotation.rest.AnonymousPostMapping;
import me.zhengjie.suli.outHouse.service.OrdersService;
import me.zhengjie.suli.roll.domain.Roll;
import me.zhengjie.suli.roll.service.RollService;
import me.zhengjie.suli.roll.service.dto.RollQueryCriteria;
import me.zhengjie.util.CommonUtil;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;

/**
* @website https://eladmin.vip
* @author WYH
* @date 2024-03-14
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "转仓管理")
@RequestMapping("/api/roll")
public class RollController {

    private final RollService rollService;
    private final CommonUtil commonUtil;
    private final OrdersService ordersService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('roll:list')")
    public void exportRoll(HttpServletResponse response, RollQueryCriteria criteria) throws IOException {
        rollService.download(rollService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询转仓")
    @ApiOperation("查询转仓")
    @PreAuthorize("@el.check('roll:list')")
    public ResponseEntity<Object> queryRoll(RollQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(rollService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增转仓")
    @ApiOperation("新增转仓")
    @PreAuthorize("@el.check('roll:add')")
    public ResponseEntity<Object> createRoll(@Validated @RequestBody Roll resources){
        return new ResponseEntity<>(rollService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改转仓")
    @ApiOperation("修改转仓")
    @PreAuthorize("@el.check('roll:edit')")
    public ResponseEntity<Object> updateRoll(@Validated @RequestBody Roll resources){
        rollService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除转仓")
    @ApiOperation("删除转仓")
    @PreAuthorize("@el.check('roll:del')")
    public ResponseEntity<Object> deleteRoll(@RequestBody String[] ids) {
        rollService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousPostMapping("/getNotRestricted")
    @Log("获取非限制库存转仓")
    @ApiOperation("获取非限制库存转仓")
    public ResponseEntity<Object> getNotRestricted(@RequestBody String param){
        return new ResponseEntity<>(rollService.getNotRestricted(param),HttpStatus.OK);
    }

    @AnonymousPostMapping("/outBoundNotRestricted")
    @Log("获取非限制库存转仓")
    @ApiOperation("获取非限制库存转仓")
    public ResponseEntity<Object> outBoundNotRestricted(@RequestBody String param){
        return new ResponseEntity<>(ordersService.outBoundNotRestricted(param),HttpStatus.OK);
    }


    @AnonymousPostMapping("/outBoundVmiToFree")
    @Log("411转仓")
    @ApiOperation("411转仓")
    public ResponseEntity<Object> outBoundVmiToFree(@RequestBody String param){
        return new ResponseEntity<>(ordersService.outBoundVmiToFree(param),HttpStatus.OK);
    }




}